package com.xlh.openai.listener;

import cn.hutool.json.JSONUtil;
import com.plexpt.chatgpt.listener.AbstractStreamListener;
import com.plexpt.chatgpt.util.SseHelper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;

/**
 * sse
 *
 * @author plexpt
 */
@Slf4j
@RequiredArgsConstructor
public class SseStreamListener extends AbstractStreamListener {

    private String baseMessage = "";

    final SseEmitter sseEmitter;


    @SneakyThrows
    @Override
    public void onMsg(String message) {
        sseEmitter.send( message.getBytes(), MediaType.APPLICATION_OCTET_STREAM );
        // SseHelper.send(sseEmitter, JSONUtil.toJsonStr(new SseVOResult().setCode("200").setText(baseMessage)));
    }

    @Override
    public void onError(Throwable throwable, String response) {
        SseHelper.complete(sseEmitter);
    }

}
